package l2ft.gameserver.ai.FakePcs;

import l2ft.commons.util.Rnd;
import l2ft.gameserver.ai.MainFakePc;
import l2ft.gameserver.geodata.GeoEngine;
import l2ft.gameserver.model.Creature;
import l2ft.gameserver.model.Skill;
import l2ft.gameserver.model.instances.NpcInstance;
import l2ft.gameserver.tables.SkillTable;

public class ArcherFakePc extends MainFakePc
{
	public ArcherFakePc(NpcInstance actor) {
		super(actor);
	}
	
	@Override
	protected void onEvtAttacked(Creature attacker, int damage)
	{
		super.onEvtAttacked(attacker, damage);
		NpcInstance actor = getActor();
		if(actor.isDead() || attacker == null || actor.getDistance(attacker) > 200)
			return;

		if(actor.isMoving)
			return;

		int posX = actor.getX();
		int posY = actor.getY();
		int posZ = actor.getZ();

		int old_posX = posX;
		int old_posY = posY;
		int old_posZ = posZ;

		int signx = posX < attacker.getX() ? -1 : 1;
		int signy = posY < attacker.getY() ? -1 : 1;
		int range = (int) (0.71 * actor.calculateAttackDelay() / 1000 * actor.getMoveSpeed());

		posX += signx * range;
		posY += signy * range;
		posZ = GeoEngine.getHeight(posX, posY, posZ, actor.getGeoIndex());

		if(GeoEngine.canMoveToCoord(old_posX, old_posY, old_posZ, posX, posY, posZ, actor.getGeoIndex()))
		{
			addTaskMove(posX, posY, posZ, false);
			addTaskAttack(attacker);
		}
	}
	
	@Override
	protected boolean defaultFightTask() 
	{
		Creature target;
		if((target = prepareTarget()) == null)
			return false;
		//getting distance
		double distance = getActor().getDistance(target);
		
		//If isnt raid and chance,
		if(!target.isRaid() && Rnd.chance(30))
		{
			Skill stun = SkillTable.getInstance().getInfo(101, 40);
			Skill hamstring = SkillTable.getInstance().getInfo(127, 14);
			//using stun
			if(canUseSkill(stun, target) && Rnd.chance(70))
				return chooseTaskAndTargets(stun, target, distance);
			//using hamstring
			else if(canUseSkill(hamstring, target))
				return chooseTaskAndTargets(hamstring, target, distance);
		}
		
		Skill lethal = SkillTable.getInstance().getInfo(343, 1);
		Skill sevenArrow = SkillTable.getInstance().getInfo(924, 1);
		//using lethal
		if(canUseSkill(lethal, target) && Rnd.chance(30))
			return chooseTaskAndTargets(lethal, target, distance);
		//using seven arrow
		else if(canUseSkill(sevenArrow, target) && Rnd.chance(30))
			return chooseTaskAndTargets(sevenArrow, target, distance);
		//normal attack
		return chooseTaskAndTargets(null, target, distance);
	}
	
	@Override
	protected boolean createNewTask()
	{
		return defaultFightTask();
	}

	@Override
	public int getRatePHYS()
	{
		return 25;
	}

	@Override
	public int getRateDOT()
	{
		return 40;
	}

	@Override
	public int getRateDEBUFF()
	{
		return 25;
	}

	@Override
	public int getRateDAM()
	{
		return 50;
	}

	@Override
	public int getRateSTUN()
	{
		return 50;
	}

	@Override
	public int getRateBUFF()
	{
		return 5;
	}

	@Override
	public int getRateHEAL()
	{
		return 50;
	}
}
